package com.zhangxun.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangxun.domain.Prod;
import com.zhangxun.domain.ProdComm;
import com.zhangxun.domain.User;
import com.zhangxun.feign.ProdMemberFeign;
import com.zhangxun.mapper.ProdMapper;
import com.zhangxun.service.ProdCommService;
import com.zhangxun.mapper.ProdCommMapper;
import com.zhangxun.vo.ProdCommData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author 张循
* @description 针对表【prod_comm(商品评论)】的数据库操作Service实现
* @createDate 2023-01-01 18:04:15
*/
@Service
public class ProdCommServiceImpl extends ServiceImpl<ProdCommMapper, ProdComm>
    implements ProdCommService{


    @Autowired
    private ProdMemberFeign prodMemberFeign;
    @Autowired
    private ProdCommMapper prodCommMapper;
    @Autowired
    private ProdMapper prodMapper;

    @Override
    public Page<ProdComm> selectPageAnCondition(Page<ProdComm> page, ProdComm prodComm) {
        //通过名称查询商品id
        List<Long> prodIdList = null;
        if(StringUtils.hasText(prodComm.getProdName())) {
            List<Prod> prodList = prodMapper.selectList(new LambdaQueryWrapper<Prod>()
                    .like(Prod::getProdName, prodComm.getProdName()));

            prodIdList = prodList.stream().map(prod->prod.getProdId()).collect(Collectors.toList());
        }


        Page<ProdComm> prodCommPage = prodCommMapper.selectPage(page,
                new LambdaQueryWrapper<ProdComm>()
                        .in(!CollectionUtils.isEmpty(prodIdList), ProdComm::getProdId, prodIdList)
                        .eq(prodComm.getStatus() != null, ProdComm::getStatus, prodComm.getStatus())
        );

        return prodCommPage;

    }

    /**
     * 查询评论总条数，好评率，好评数，中评数，差评数，有图数量。
     * @param prodId
     * @return
     */
    @Override
    public ProdCommData selectCommNumDetails(Long prodId) {
        ProdCommData prodCommData = new ProdCommData();
        //查询出商品评论
        List<ProdComm> prodCommList = prodCommMapper.selectList(new LambdaQueryWrapper<ProdComm>()
                .eq(ProdComm::getProdId, prodId)
                .eq(ProdComm::getStatus, 1));

        //筛选给结果赋值
        if(CollectionUtil.isNotEmpty(prodCommList)) {
            int total = prodCommList.size();
            Long praiseNumber = prodCommList.stream().filter(prodComm -> prodComm.getEvaluate().equals(0)).count();
            Long secondNumber = prodCommList.stream().filter(prodComm -> prodComm.getEvaluate().equals(1)).count();
            Long negativeNumber = prodCommList.stream().filter(prodComm -> prodComm.getEvaluate().equals(2)).count();
            Long picNumber = prodCommList.stream().filter(prodComm -> StringUtils.hasText(prodComm.getPics())).count();


            BigDecimal positiveRating = new BigDecimal(praiseNumber)
                    .divide(new BigDecimal(total), 2, BigDecimal.ROUND_HALF_UP)
                    .multiply(new BigDecimal(100));
            prodCommData.setNumber(total);
            prodCommData.setPraiseNumber(praiseNumber);
            prodCommData.setSecondaryNumber(secondNumber);
            prodCommData.setNegativeNumber(negativeNumber);
            prodCommData.setPicNumber(picNumber);
            prodCommData.setPositiveRating(positiveRating);
        }

        return prodCommData;
    }

    /**
     * 通过条件分页查询评论
     * @param page 分页信息
     * @param prodId 评论对应的商品id
     * @param evaluate -1全部，0好评，1中评，2差评，3有图
     * @return
     */
    @Override
    public Page<ProdComm> pageByCondition(Page<ProdComm> page, Long prodId, Integer evaluate) {

        page = prodCommMapper.selectPage(page, new LambdaQueryWrapper<ProdComm>()
                .eq(ProdComm::getProdId, prodId)
                .eq(ProdComm::getStatus,1)
                .eq(evaluate != -1 & evaluate != 3, ProdComm::getEvaluate, evaluate)
                .isNotNull(evaluate == 3, ProdComm::getPics));

        List<ProdComm> prodCommList = page.getRecords();
        if(CollectionUtil.isNotEmpty(prodCommList)) {

            //获取评论用户的id,去重
            List<String> userIdList = prodCommList.stream().map(ProdComm::getUserId).distinct().
                    collect(Collectors.toList());
            //远程调用获取用户集合
            List<User> userList = prodMemberFeign.selectUserByIdS(userIdList);

            //转换为map为后面获取user做转换
            Map<String, User> userMap = userList.stream().collect(Collectors.toMap(User::getUserId, user -> user));

            //遍历评论集合，给每个评论设置用户昵称和头像
            prodCommList.stream().forEach(prodComm->{
                //获取对应评论的用户
                User user = userMap.get(prodComm.getUserId());
                //设置头像
                prodComm.setPic(user.getPic());
                //判断评论是否为匿名评论
                Integer anonymous = prodComm.getIsAnonymous();

                if(anonymous == 1){//是
                    //设置昵称为匿名用户
                    prodComm.setNickName("匿名用户");
                }else{//否
                    //设置昵称并将
                    String nickName = user.getNickName();
                    String safeNickName = new StringBuilder(nickName)
                            .replace(0, nickName.length() - 1, "*****").toString();
                    prodComm.setNickName(safeNickName);
                }
            });
        }

        return page;
    }
}




